<?php
class LoginController extends Controller {

    public $layout = '//layouts/login';

    public function filters() {
        return array(
            //'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    public function init() {
        Yii::app()->clientScript->scriptMap = array('jquery.js' => false, 'jquery.yii.js' => false, 'jquery.yiiactiveform.js' => false);
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view', 'iniciarSesionUsuario'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array(),
                'users'=>array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array(),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    public function actionIndex() {
        Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/login/index.js', CClientScript::POS_END);
        $model = new LoginForm();

        $resultado = array(
            'status' => false
        );

        $this->render('index', array(
            'model' => $model
        ));
    }

    public function actionIniciarSesionUsuario() {
        $resultado['status'] = true;

        if(isset($_GET['callback'])) {
            echo $_GET['callback'] . "(" . CJSON::encode($resultado) . ")";
        } else {
            echo CJSON::encode($resultado);
        }
    }

    public function actionEliminarSesionUsuario() {
        unset($_SESSION['Usuario']);

        $this->redirect(array('login/'));
    }
}